#include "dswldclient.h"

CDswLDClient::CDswLDClient(LPST_ENV_T pEnv, char * pHost, int nPort, bool bIsV3, LPST_ALARM_NODE_T pAlarmNode)
: CDswtcpclient_base(pEnv, pHost, nPort)
, m_bIsV3(bIsV3)
{
	LOG4CPLUS_DEBUG(log_dsw, "Create CDswLDClient class! host = " << pHost << ":" << nPort);
	memcpy(&m_nAlarmNode, pAlarmNode, sizeof(m_nAlarmNode));
}

CDswLDClient::~CDswLDClient()
{
	LOG4CPLUS_DEBUG(log_dsw, "Destroy CDswLDClient class!");
}

void CDswLDClient::OnConnect(struct bufferevent *bev, short events)
{
	CMarkup xml;
	xml.SetDoc("");
	xml.AddElem("LD_DATA");
	if (m_bIsV3){
		xml.AddAttrib("ZONEID", m_nAlarmNode.sId);
		xml.AddAttrib("TRIGVAL", m_nAlarmNode.nTrigVal);
//		xml.AddAttrib("TRIGTM", m_nAlarmNode.nTrigTime - 3600 * 8);
		xml.AddAttrib("TRIGTM", m_nAlarmNode.nTrigTime);
		xml.AddAttrib("ACTION", m_nAlarmNode.nAction);
		xml.AddAttrib("ZONETYPE", 0);
		int nSendLen = strlen(xml.GetDoc().c_str());
		evbuffer_add(bufferevent_get_output(m_pBev), &nSendLen, sizeof(int));
		evbuffer_add(bufferevent_get_output(m_pBev), xml.GetDoc().c_str(), nSendLen);
	}
	else{
		xml.AddChildElem("RFID", m_nAlarmNode.sId);
		xml.AddChildElem("AVI", (m_nAlarmNode.nAction & EN_VIDEO) ? 1 : 0);
		xml.AddChildElem("JPG", (m_nAlarmNode.nAction & EN_PIC) ? 1 : 0);
		evbuffer_add(bufferevent_get_output(bev), xml.GetDoc().c_str(), strlen(xml.GetDoc().c_str()));
	}
}



